package esClient;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2020/2/14.
 * 原始esclient 通过http拼接请求 并解析响应
 * 这里由于es使用的httpcomponents版本高于solr 因此注释了solr pom信息
 */
public class LowClient {
    private  RestClient restClient;

    //初始化
    @Before
    public void init(){
        RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("192.168.32.128",9200,"http"));
        restClient = restClientBuilder.build();
    }
    //关闭
    @After
    public void close() throws Exception{
        restClient.close();
    }
    //查询es状态
    @Test
    public void getEsInfo() throws Exception{
        Request request = new Request("GET","/_cluster/state");
        request.addParameter("pretty", "true");
        Response response = this.restClient.performRequest(request);
        System.out.println(response.getStatusLine());
        System.out.println(EntityUtils.toString(response.getEntity()));
    }
    //通过id查询
    @Test
    public void getById() throws Exception{
        Request request = new Request("GET","es/user/1001");
        request.addParameter("pretty", "true");
        Response response = this.restClient.performRequest(request);
        System.out.println(response.getStatusLine());
        System.out.println(EntityUtils.toString(response.getEntity()));
    }
    //新增数据
    @Test
    public void addDate() throws Exception{
        Request request = new Request("POST","es/user");
        Map<String,Object> data = new HashMap<>();
        data.put("id","1002");
        data.put("name","焦鑫");
        data.put("age", "27");
        data.put("sex", "男");
        ObjectMapper mapper = new ObjectMapper();
        request.setJsonEntity(mapper.writeValueAsString(data));
        Response response = this.restClient.performRequest(request);
        System.out.println(response.getStatusLine());
        System.out.println(EntityUtils.toString(response.getEntity()));
    }
    //搜索
    @Test
    public void serachDate() throws Exception{
        Request request = new Request("POST","es/user/_search");
        request.addParameter("pretty", "true");
        String serachJson ="{\"query\":{\"match\":{\"name\":\"文秀\"}}}";
        request.setJsonEntity(serachJson);
        Response response = this.restClient.performRequest(request);
        ObjectMapper mapper = new ObjectMapper();
        String json = EntityUtils.toString(response.getEntity());
        System.out.println(json);
        JsonNode jsonNode = mapper.readTree(json);
        int total = jsonNode.get("hits").get("total").get("value").asInt();
        System.out.println(total);
        ArrayNode hits =(ArrayNode)jsonNode.get("hits").get("hits");
        for (JsonNode hit : hits){
            System.out.println(hit.get("_source").get("name").asText());
        }
    }
}
